package com.wgy.designmode.structure.filter;

import com.wgy.designmode.structure.filter.criteria.*;
import com.wgy.designmode.structure.filter.person.Person;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器模式
 * 示例：操作数据结构：人员集合     过滤逻辑多样：男、女、单身男、单身女、已婚男、已婚女等
 * 场景：当对象集合需要根据不同的标准进行筛选时；当筛选逻辑可能变化，或者需要动态地组合多个筛选条件时。
 */
public class FilterPatternDemo {

    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();

        persons.add(new Person("Robert", "Male", "Single"));
        persons.add(new Person("John", "Male", "Married"));
        persons.add(new Person("Laura", "Female", "Married"));
        persons.add(new Person("Diana", "Female", "Single"));
        persons.add(new Person("Mike", "Male", "Single"));
        persons.add(new Person("Bobby", "Male", "Single"));

        Criteria male = new CriteriaMale();
        Criteria female = new CriteriaFemale();
        Criteria single = new CriteriaSingle();
        Criteria singleMale = new AndCriteria(single, male);
        Criteria singleOrFemale = new OrCriteria(single, female);

        System.out.println("Males: ");
        printPersons(male.meetCriteria(persons));

        System.out.println("\nFemales: ");
        printPersons(female.meetCriteria(persons));

        System.out.println("\nSingle Males: ");
        printPersons(singleMale.meetCriteria(persons));

        System.out.println("\nSingle Or Females: ");
        printPersons(singleOrFemale.meetCriteria(persons));

    }

    public static void printPersons(List<Person> persons) {
        for (Person person : persons) {
            System.out.println(person);
        }
    }
}
